package com.rzico.sbl.ui.manager

import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.R
import com.rzico.sbl.model.*
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import org.json.JSONObject

class ManagerViewModel : CommonViewModel() {

    val mBarrelCount = MutableLiveData<Array<Int>>()

    fun generateList() = arrayListOf(
        ManagerDivider("会员"),
        ManagerContent("会员", R.mipmap.icon_manager08),
        ManagerContent("添加会员", R.mipmap.icon_manager10),
        ManagerContent("休眠会员", R.mipmap.icon_manager09),
        ManagerContent("水票提醒", R.mipmap.icon_manager01),
        ManagerDivider("员工"),
        ManagerContent("员工", R.mipmap.icon_manager02),
        ManagerContent("添加员工", R.mipmap.icon_manager07),
        ManagerContent("实时位置", R.mipmap.icon_manager03),
        ManagerContent("员工围栏", R.mipmap.icon_manager29),
        ManagerDivider("水桶"),
        ManagerContent("押桶", R.mipmap.icon_manager25),
        ManagerContent("退桶", R.mipmap.icon_manager24),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("自提"),
        ManagerContent("自提点", R.mipmap.icon_manager26),
        ManagerContent(),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("优惠券"),
        ManagerContent("优惠券", R.mipmap.icon_manager11),
        ManagerContent("核销券", R.mipmap.icon_manager12),
        ManagerContent("添加券", R.mipmap.icon_manager13),
        ManagerContent(),
        ManagerDivider("发票"),
        ManagerContent("普通发票", R.mipmap.icon_manager14),
        ManagerContent("专用发票", R.mipmap.icon_manager15),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider()
    )

    /**
     * 商品分类
     */
    fun productCat() =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(BaseUrl.productCat)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    /**
     * 发票列表
     */
    fun taxList(
        page: Int,
        status: String,
        onSuccess: (ArrayList<TaxData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<TaxData>>>(BaseUrl.mngTax)
            .params(
                getParams(
                    "type" to status,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<TaxData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 获取退桶单数量
     */
    fun getBarrelCount() {
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.mngBarrelCut,
            onSuccess = {
                val obj = JSONObject(it)

                mBarrelCount.value = arrayOf(
                    obj.optNotNull("unconfirmed").intValue,
                    obj.optNotNull("dispatch").intValue,
                    obj.optNotNull("delivery").intValue,
                    obj.optNotNull("completed").intValue
                )
            }
        )
    }

}